﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Script.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;


namespace SyncServer
{
    public partial class TestDownload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           // if (!IsPostBack)
            {
                Jumpton.SFA.Service.Service service = new Jumpton.SFA.Service.Service();
                var user = new LoginUser();
                user.UserName = "kxm";
                user.UserPwd="698D51A19D8A121CE581499D7B701668";
                user.VersionCode = "0.35";
                user.PlatformCode = "android";
                Response.Write(service.Login(user));
            }
        }

        protected void RequestTest()
        {
            string json = "\"UserToken\":\"{0}\",\"Requestkey\":\"{1}\",\"Tables\":[{2}],\"Version\":\"{3}\",\"Gzip\":\"{4}\"";
            string[] tables = txtTableList.Text.Split(new string[] { "\r\n" },StringSplitOptions.RemoveEmptyEntries);
            string table = "";
            foreach (string t in tables)
            {
                table += "\"" + t + "\",";
            }
            table = table.TrimEnd(',');
            json = string.Format(json, txtToken.Text, Jumpton.Common.Functions.EncryptString(txtToken.Text+ txtVersion.Text + txtRequestKey.Text), table, txtVersion.Text, ddl_zip.SelectedValue);
            json = "{" + json + "}";
            //JavaScriptSerializer ser = new JavaScriptSerializer();
            //RequestHeader header = ser.Deserialize<RequestHeader>(json);
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(txtAddress.Text +"download.aspx");
            byte[] data = System.Text.Encoding.UTF8.GetBytes(json);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "post";
            request.ContentLength = data.Length;
            System.IO.Stream stream = request.GetRequestStream();
            stream.Write(data, 0, data.Length);
            stream.Flush();
            stream.Close();
            System.Net.WebResponse response = request.GetResponse();
            stream = response.GetResponseStream();


            if(ddl_zip.SelectedValue=="false")
                lblContent.Text = Common.ToString(stream);//无压缩
            else
                lblContent.Text=Common.Decompress(stream);//压缩/
            


           
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            RequestTest();


            //System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(txtAddress.Text + string.Format("download.ashx?pass={0}&user_code={1}&version={2}", txtPassword.Text, txtUserName.Text, txtVersion.Text));
            //byte[] data = System.Text.Encoding.UTF8.GetBytes(txtTableList.Text + "\r\n");
            //request.ContentType = "application/x-www-form-urlencoded";
            //request.Method = "post";
            //request.ContentLength = data.Length;
            //System.IO.Stream stream = request.GetRequestStream();
            //stream.Write(data, 0, data.Length);
            //stream.Flush();
            //stream.Close();
            //System.Net.WebResponse response = request.GetResponse();
            //stream = response.GetResponseStream();
           

            //lblContent.Text = Common.Decompress(stream);
            
        }


        /// <summary>
        /// 解压缩数据流
        /// </summary>
        /// <param name="inputStream"></param>
        /// <returns></returns>
        private byte[] Decompression(System.IO.Stream inputStream)
        {
            try
            {

                ICSharpCode.SharpZipLib.GZip.GZipInputStream stream = new ICSharpCode.SharpZipLib.GZip.GZipInputStream(inputStream);

                System.IO.MemoryStream output = new System.IO.MemoryStream();
                byte[] buffer = new byte[4096];
                int read = -1;
                while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    output.Write(buffer, 0, read);

                }
                stream.Close();
                return output.ToArray();
            }
            catch (Exception err)
            {
                throw new Exception("CON_ERR_COMPRESS");
            }
        }
    }
}
